package com.usian.wemedia.service.impl;/*
 *文件名：WmMaterialServiceImpl
 *创建者：宋璟
 *创建时间:2023/2/11 11:13
 *描述: TODO
 */


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import groovy.util.logging.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    FastDFSClient fastDFSClient;

    @Value("${fdfs.url}")
    private String baseUrl;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        if (multipartFile == null) return errorParam;
        String s = null;
        try {
            s = fastDFSClient.uploadFile(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        WmMaterial material = new WmMaterial();
        material.setUserId(ThreadLocalUtils.getUserId());
        material.setUrl(s);
        material.setType((short) 0);
        material.setIsCollection((short) 0);
        material.setCreatedTime(new Date());
        save(material);
        return ResponseResult.okResult(baseUrl+s);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //检查参数
        if (dto == null) return paramError();
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getUserId,ThreadLocalUtils.getUserId());
        //查询 收藏地址值是否相等
        wrapper.eq(dto.getIsCollection() != null,WmMaterial::getIsCollection,dto.getIsCollection());
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        ResponseResult pageData = getPage(dto,wrapper);



        List<WmMaterial> list = (List<WmMaterial>) pageData.getData();


        for (WmMaterial wmMaterial : list) {
            wmMaterial.setUrl(baseUrl+wmMaterial.getUrl());
        }


        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult delPicture(Integer id) {
        if (id ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmNewsMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmNewsMaterial::getId,id);
        Integer count = wmNewsMaterialMapper.selectCount(lambdaQueryWrapper);
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"当前图片被引用");
        }

        fastDFSClient.delFile(wmMaterial.getUrl());
        removeById(id);
        return success;
    }

    @Override
    public ResponseResult updateStatus(Integer id, Short types) {
        if (id ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer uid = WmThreadLocalUtils.getUser().getId();
        update(Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getIsCollection,types)
                .eq(WmMaterial::getId,id).eq(WmMaterial::getUserId,uid));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

























    /*@Autowired
    FastDFSClient fastDFSClient;

    @Value("${fdfs.url}")
    private String fileServerUrl;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //1.检查参数
        if(multipartFile == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到fasfdfs
        String fileId = null;
        try {
            fileId = fastDFSClient.uploadFile(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        //3.保存素材数据到表中 wm_material

        WmUser user = WmThreadLocalUtils.getUser();

        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(fileId);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setUserId(user.getId());
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);
        //拼接图片路径
        wmMaterial.setUrl(fileServerUrl+fileId);
        return ResponseResult.okResult(wmMaterial);
    }*/
}
